﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Common;
using Sausage.Web.Code;
using Sausage.Web.DataModel;
using Sausage.Web.Domain;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 养护材料控制器
    /// </summary>
    /// <remarks></remarks>
    /// <history>
    /// [zanezeng]               2012/02/22 12:24    创建
    /// </history>
    public class MaintainMaterialController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Submit()
        {
            return View();
        }

        /// <summary>
        /// 养护工程量审核。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Audit()
        {
            return View();
        }

        /// <summary>
        /// 养护工程量确认。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Confirm()
        {
            return View();
        }

        /// <summary>
        /// 查看养护材料。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult ViewQuantityMaterial(Guid quantityID)
        {
            //返回视图
            return View(MaintainQuantityHelper.GetMaintainQuantityByID(quantityID));
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Select(JosnPagingQueryModel query)
        {
            var result = MaintainMaterialHelper.Select(query);

            //返回结果
            return new JsonNetResult()
                       {
                           Data = new JsonResultViewModel
                                      {
                                          success = true,
                                          data = result.Data,
                                          total = result.Total,
                                          message = "养护材料加载成功！"
                                      },
                           JsonRequestBehavior = JsonRequestBehavior.AllowGet
                       };
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult SubmitSelect(JosnPagingQueryModel query)
        {
            var result = MaintainMaterialHelper.SubmitSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护材料加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult AuditSelect(JosnPagingQueryModel query)
        {
            var result = MaintainMaterialHelper.AuditSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护材料加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>
        /// 查询的工程项目信息列表
        /// </returns>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ConfirmSelect(JosnPagingQueryModel query)
        {
            var result = MaintainMaterialHelper.ConfirmSelect(query);

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    data = result.Data,
                    total = result.Total,
                    message = "养护材料加载成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        /// <summary>
        /// 详细
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Details(Guid id)
        {
            var item = MaintainMaterialHelper.GetMaintainMaterialByID(id);
            if (item == null)
                return Json(new JsonResultViewModel { success = true, message = "找不到此养护材料！" },
                            JsonRequestBehavior.AllowGet);

            var data = new
            {
                item.MaintainMaterialID,
                item.QuantityID,
                item.OrganizationID,
                Organization = item.Organization.OrganizationName,
                MaintainCode = item.MaintainQuantity.MaintainCode,
                item.MaterialID,
                Material = item.Material.MaterialName,
                Quantity = item.Quantity,
                Unit = item.Material.Unit,
                Price = item.Price,
                Amount = item.Amount,
                CreateDate = item.CreateDate,
                Applicant = item.Applicant,
                ApplicationDate = item.ApplicationDate,
                Auditor = item.Auditor,
                AuditedDate = item.AuditedDate,
                Approver = item.Approver,
                ApprovedDate = item.ApprovedDate,
                State = item.State,
                IsDelay = item.IsDelay,
                Memo = item.Memo
            };

            //返回结果
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel
                {
                    success = true,
                    total = 1,
                    data = data,
                    message = "加载养护材料成功！"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Delete(MaintainMaterialListRow data)
        {
            MaintainMaterialHelper.DeleteMaintainMaterial(data.MaintainMaterialID);
            return Json(new JsonResultViewModel { success = true, message = "养护材料删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Update(MaintainMaterialListRow data)
        {
            MaintainMaterialHelper.UpdateMaintainMaterial(data);
            return Json(new JsonResultViewModel { success = true, message = "养护材料更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <returns>执行结果</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult UpdateState(Guid id, int state)
        {
            MaintainMaterialHelper.UpdateMaintainMaterialState(id, state);
            return Json(new JsonResultViewModel { success = true, message = "养护材料状态更新成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <returns>创建结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult Create(MaintainMaterialListRow data)
        {
            string errorMessage;
            if (!Helper.CouldSet(data.ApplicationDate, out errorMessage))
                return Json(new JsonResultViewModel { success = false, message = errorMessage }, JsonRequestBehavior.AllowGet);

            MaintainMaterialHelper.CreateMaintainMaterial(data);
            return Json(new JsonResultViewModel { success = true, message = "养护材料创建成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="quantityID">The quantity ID.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteSubmit(Guid[] materials)
        {

            MaintainMaterialHelper.ExecuteSubmit(materials);
            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "养护材料提交成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="materials">The materials.</param>
        /// <param name="name">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteAudit(Guid[] materials, bool passed, string comment)
        {
            MaintainMaterialHelper.ExecuteAudit(materials, passed, comment);
            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "养护材料审核成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 批准
        /// </summary>
        /// <param name="materials">The materials.</param>
        /// <param name="auditor">The auditor.</param>
        /// <returns>
        /// 执行结果
        /// </returns>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("*")]
        public JsonResult ExecuteConfirm(Guid[] materials, bool passed, string comment)
        {
            MaintainMaterialHelper.ExecuteConfirm(materials, passed, comment);
            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "养护材料审核成功！" }, JsonRequestBehavior.AllowGet);
        }
    }
}
